package org.jeecg.modules.quartz.job;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.demo.decoration.entity.DecorationDeposit;
import org.jeecg.modules.demo.decoration.entity.Yajin;
import org.jeecg.modules.demo.decoration.mapper.YajinMapper;
import org.jeecg.modules.demo.decoration.service.IDecorationDepositService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ZxbzjJob implements Job {
    @Autowired
    private YajinMapper yajinMapper;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("11111111111111111111111");
//        YajinMapper yajinMapper = SpringContextUtils.getBean("yajinMapper", YajinMapper.class);
        IDecorationDepositService hello = SpringContextUtils.getBean("hello", IDecorationDepositService.class);
        ISysDepartService iSysDepartService = SpringContextUtils.getBean("sysDepartServiceImpl", ISysDepartService.class);
        QueryWrapper<Yajin> queryWrapperYajin = new QueryWrapper<>();
        queryWrapperYajin.isNotNull("yezhu");
        List<Yajin> yajins = yajinMapper.selectList(queryWrapperYajin);
        int a = 0;
        int b = 0;
        for (Yajin yajin : yajins) {
            //获取条件构造器 获取押金id为某值的对象
            QueryWrapper<DecorationDeposit> queryWrapper2 = new QueryWrapper<>();
            //获取房产编号,判断是否为空的操作
            String communityName1 = yajin.getCommunityName();
            //获取首字母大写
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < communityName1.length(); i++) {
                char c = communityName1.charAt(i);
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) { // 判断是否为中文字符
                    sb2.append(Character.toUpperCase(PinyinHelper.toHanyuPinyinStringArray(c)[0].charAt(0)));
                }
            }
            String xiaoqu2 = sb2.toString();
            xiaoqu2 += "-"+yajin.getPropertyNumber();
            queryWrapper2.eq("property_number",xiaoqu2);
            List<DecorationDeposit> list1 = hello.list(queryWrapper2);
            //如果数据已经存在 根据yajinid 进行数据编辑 如果不存在则进行数据保存
            if (list1.size() != 0){
                DecorationDeposit decorationDeposit = list1.get(0);
                decorationDeposit.setOwnerName(xiaoqu2);
                //设置退保证金的金额
                String tuibaozhengjin = yajin.getTuibaozhengjin();
                if (StrUtil.isNotEmpty(tuibaozhengjin)){
                    decorationDeposit.setTuibaozhengjin(Double.parseDouble(tuibaozhengjin));
                }
                //设置退保证金的时间
                decorationDeposit.setTuidate(yajin.getTuidata());
                //设置业主姓名
                decorationDeposit.setOwnerName(yajin.getOwnerName());
                //进行数据覆盖
                hello.updateById(decorationDeposit);
//                hello.updateByYajin(decorationDeposit);
                a++;
            }else {
                //数据不存在进行数据保存
                DecorationDeposit decorationDeposit = new DecorationDeposit();
                decorationDeposit.setYajinId(yajin.getId());
                //设置部门id
                String communityName = yajin.getCommunityName();
//                decorationDeposit.setCommunityName(communityName);
                QueryWrapper<SysDepart> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("depart_name",yajin.getCommunityName());
                List<SysDepart> list = iSysDepartService.list(queryWrapper);
                if (!list.isEmpty()){
                    decorationDeposit.setCommunityName(list.get(0).getId());
                }

                //设置房产编号
                //获取首字母大写
//                StringBuilder sb = new StringBuilder();
//                for (int i = 0; i < communityName.length(); i++) {
//                    char c = communityName.charAt(i);
//                    if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) { // 判断是否为中文字符
//                        sb.append(Character.toUpperCase(PinyinHelper.toHanyuPinyinStringArray(c)[0].charAt(0)));
//                    }
//                }
//                String xiaoqu = sb.toString();
//                xiaoqu += "-"+yajin.getPropertyNumber();
                decorationDeposit.setPropertyNumber(xiaoqu2);
                //设置业主姓名
                decorationDeposit.setOwnerName(yajin.getOwnerName());
                if (StrUtil.isNotEmpty(yajin.getDepositCollection())){
                    decorationDeposit.setDepositCollection(Double.parseDouble(yajin.getDepositCollection()));
                }
                //设置收取时间
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
                try {
                    if (StrUtil.isNotEmpty(yajin.getCollectionDate())){
                        Date date = dateFormat.parse(yajin.getCollectionDate());
                        System.out.println("Date object: " + date);
                        decorationDeposit.setCollectionDate(date);
                    }
                } catch (ParseException e) {
                    System.out.println("Error parsing date: " + e.getMessage());
                }
                //设置退还时间
                try {
                    if (StrUtil.isNotEmpty(yajin.getReturnDate())){
                        Date date = dateFormat.parse(yajin.getReturnDate());
                        System.out.println("Date object: " + date);
                        decorationDeposit.setReturnDate(date);
                    }
                } catch (ParseException e) {
                    System.out.println("Error parsing date: " + e.getMessage());
                }
                //设置推取保证金
                String tuibaozhengjin = yajin.getTuibaozhengjin();
                if (StrUtil.isNotEmpty(tuibaozhengjin)){
                    decorationDeposit.setTuibaozhengjin(Double.parseDouble(tuibaozhengjin));
                }
                //设置退保证金的时间
                decorationDeposit.setTuidate(yajin.getTuidata());
                //进行数据添加
                boolean insert = hello.save(decorationDeposit);
                if (insert){
                    b++;
                }
            }
        }
        System.out.println("a"+a);
        System.out.println("b"+b);
    }
}
